package com.gtq.uhome.service.serviceImpl;

import com.gtq.uhome.mapper.OrderMapper;
import com.gtq.uhome.mapper.UserOrderRelationMapper;
import com.gtq.uhome.service.AuditManagementService;
import com.gtq.uhome.vo.AuditManagement;
import io.swagger.annotations.ApiModel;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * 待审核列表
 */
@Service("auditManagementService")
@Transactional(propagation = Propagation. REQUIRED, isolation = Isolation.READ_COMMITTED, rollbackFor = Throwable.class)
public class AuditManagementServiceImpl implements AuditManagementService {

    @Resource
    private UserOrderRelationMapper userOrderRelationMapper;//实例化

    @Resource
    private OrderMapper orderMapper;

    public UserOrderRelationMapper getUserOrderRelationMapper() {
        return userOrderRelationMapper;
    }

    /**
     * 查询待审核列表所有内容
     * @return AuditManagement结果集
     */
    @Override
    public List<AuditManagement> findByAll() {

        List<AuditManagement> byAll = userOrderRelationMapper.findByAll();
        Date date =new Date();
        // 计算等待时间
        for (AuditManagement auditManagement : byAll) {
            long start = date.getTime();
            // 获取订单提交时间毫秒数
            long end = auditManagement.getBornTime().getTime();
            // 计算等待时间
            long day = (start - end) / (1000 * 60 * 60 * 24);
            long h = ((start - end)) % (1000 * 60 * 60 * 24) / (1000 * 60 * 60);
            long minutes = ((start - end) % (1000 * 60 * 60)) / (1000 * 60);
            // 给等待时间赋值
            auditManagement.setWaitTime(day+"天"+h+"小时"+minutes+"分钟");
        }
        return byAll;
    }

    /**
     * 按条件查询待审核列表所有内容
     * @param auditManagements
     * @return AuditManagement结果集
     */
    @Override
    public List<AuditManagement> findBy(AuditManagement auditManagements) {
        List<AuditManagement> by = userOrderRelationMapper.findBy(auditManagements);
        Date date =new Date();
        // 计算等待时间
        for (AuditManagement auditManagement : by) {
            // 获取当前时间毫秒数
            long start = date.getTime();
            // 获取订单提交时间毫秒数
            long end = auditManagement.getBornTime().getTime();
            // 计算等待时间
            long day = (start - end) / (1000 * 60 * 60 * 24);
            long h = ((start - end) % (1000 *3600 * 24)) / (1000 * 3600);
            long minutes = ((start - end) % (1000 * 3600)) / (1000 * 60);
            // 给等待时间赋值
            auditManagement.setWaitTime(day+"天"+h+"小时"+minutes+"分钟");
        }
        return by;
    }

    /**
     * 待审核列表,批量删除
     * @param auditManagement
     * @return
     */
    @Override
    public Boolean deleteOrder(List<AuditManagement> auditManagement) {
        Boolean delete =false;
        for (AuditManagement management : auditManagement) {
            Integer integer = orderMapper.deleteOrder(management);
            if (integer==1){
                delete=true;
            }
        }
        return delete;
    }
}
